package xin.ham.aprs4gserv.entity;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Arrays;

@Data
@Slf4j
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private String retCode;

    private String retMsg;

    private T body;

    /**
     * ***************静态方法*******************
     */
    /**
     *
     * 成功
     * @param <T> 返回值类型
     * @return {@link Response}
     */
    public static <T> Response<T> ok() {
        return ok(null);
    }

    public static <T> Response<T> ok(T body) {
        return ok(body, "ok");
    }

    public static <T> Response<T> ok(T body, String msg) {
        return result(body, ResultCodeEnum.CODE_200.code(), msg);
    }

    /**
     * 失败
     * @param <T> 返回值类型
     * @return {@link Response}
     */
    public static <T> Response<T> error() {
        return error("error");
    }

    public static <T> Response<T> error(String msg) {
        return result(ResultCodeEnum.SYS_ERROR.code(), msg);
    }

    public static <T> Response<T> error(Class<T> clazz, String msg) {
        return result(ResultCodeEnum.SYS_ERROR.code(), msg);
    }

    /**
     * 入参格式错误
     * @author Ivan yu
     * @param <T>
     * @return
     */
    public static <T> Response<T> paramError(){
        return Response.result(ResultCodeEnum.PARAM_ERROR.code(), ResultCodeEnum.PARAM_ERROR.msg());
    }

    public static <T> Response<T> paramError(String... param){
        return Response.result(ResultCodeEnum.PARAM_ERROR.code(),"错误的入参格式,错误参数: "+ Arrays.toString(param));
    }
    /**
     *
     * @param <T> 返回泛型
     * @param body 主体
     * @param retCode 返回码
     * @param retMsg 返回信息
     * @return {@link Response}
     */
    public static <T> Response<T> result(T body, String retCode, String retMsg) {
        Response<T> apiResult = new Response<>();
        apiResult.setBody(body);
        apiResult.setRetCode(retCode);
        apiResult.setRetMsg(retMsg);
        return apiResult;
    }

    private void setBody(T body) {
        this.body = body;
    }
    private void setRetCode(String retCode){
        this.retCode = retCode;
    }
    private void setRetMsg(String retMsg){
        this.retMsg = retMsg;
    }

    public static <T> Response<T> result(ResultCodeEnum codeEnum) {
        return result(null, codeEnum.code(), codeEnum.msg());
    }

    public static <T> Response<T> result(String code, String msg) {
        return result(null, code, msg);
    }

    /**
     * 返回状态码
     * @param body
     * @param codeEnum
     * @param <T>
     * @return
     */
    public static <T> Response<T> result(T body, ResultCodeEnum codeEnum) {
        return result(body, codeEnum.code(), codeEnum.msg());
    }

    /**
     * 是否是成功的返回
     * @param response
     * @return
     */
    public static boolean isSuccess(Response response){
        if(ResultCodeEnum.CODE_200.code().equals(response.getRetCode())){
            return true;
        }else{
            return false;
        }
    }

    public static Response status(boolean flag) {
        return flag ? Response.ok("成功") : Response.error("操作失败");
    }
}
